﻿#ifndef DATEBASE_H
#define DATEBASE_H


#include <QString>
#include <QDebug>
#include <QLatin1String>
#include <QVariant>
#include <QXmlStreamAttributes>
#include <QTextCodec>
#include <QDateTime>
#include <QSqlDatabase>
#include <QBuffer>
#include <QSqlQuery>
#include <QImage>
#include <QSqlError>
#include<QMutex>
#include "systemfile.h"
#define MAX_FIND_DLY    10       //上传时最大查找图片时间
#define BANG_ONE 0
#define BANG_FU 1
typedef struct {  //车信息结构
        QString head;
        QString plate;
        QString PhotoTime;
        QString PhotoLane;
        QString plateColor;
        QString lane;
        QDateTime dt;
        int timePoint;
        QString p1;
        QString p2;
}CarInfStr;
typedef struct {  //车信息结构
        QString lStrDwid;
        QString JiLuId;
        QString ChePai;
        QString ChePaiYanSe;
        QString ZhouShu;
        QString ZhouXing;
        QString XianZhong;
        QString MaoZhong;
        QString ChaoZai;
        QString ChaoZaiLv;

        QString CheChang;
        QString CheKuan;
        QString CheGao;
        QString CheTouTu;
        QString ChePaiTu;
        QString QuanJingTu;
        QString YuLiuTu1;
        QString YuLiuTu2;
        QString CheDaoHao;
        QString ChengLeiXing;
        QString FuBang;
        QString JiaoYanWei;
        //车辆信息数据
        QString cccdjbh,cqybh,dwlx,ccs,cpz;
        //出厂登记编号,单位编号,单位类型,车速,皮重
        QString cjz,ccc,cck,ccg,cxszh;
        //净重,车长,车宽,车高,车辆行驶证编号
        QString cyszh,ccyrdz,clxdh,czzhw,chclx;
        //车辆运输证编号,,,货物名称,货车类型
        QString czzqy,cmdd,cjsyxm,cjsydh,cjsysfzh;
        //装载企业,目的地,驾驶员姓名,驾驶员电话,驾驶员身份证号
        QString cjsyzz,cjsyjszh,cjsycyzgzbh,cssdw,cssdwjgdm;
        //驾驶员住址,驾驶员驾驶证号,驾驶员从业资格证编号,所属单位（企业/车主）,,
        QString cssdwdz,cssdwfzr,cssdwfzrdh,cssdwzcr,cczr;
        //所属单位地址,所属单位负责人,所属单位负责人电话,所属单位治超负责人,称重人
        QString cczsj,ccdh,cclx,cbz;
        //称重时间,,称类型,,
}RecordInf;

Q_DECLARE_METATYPE(RecordInf)
typedef struct {  //称重信息结构
    float weightValue;
}
WeightInfo;

typedef struct {
    float DyweightValue;
    int DyaxisNum;
}
DynamicWeightInfo;

typedef struct {  //轮轴信息结构
    int maxWeightValue;
    QString axisGbType;   //国标类型
    QString axisUserType; //自定义类型
    int axisNum;
}
AxisInfo;

typedef struct{
    //QString carNum;  //car num
    QString plate;  //car plate num
    int carType;
    float carTare; //car tare
}
CardInfo;
typedef struct{
    int car_axis;
    int max_weight;
    QString desc;
    QString car_shapes;
}
CarType;
class datebase
{
public:
    static datebase* GetInstance();
    QSqlDatabase mySql_db;
private:
    datebase();   
    static datebase *m_db;  // 指向单例对象的指针
    static QMutex m_mutex;
public slots:
    bool IsQueryCardInfo(QSqlDatabase* db,CardInfo Info,QString card_id);

    bool IsUpdateCardInfo(QSqlDatabase* db, QString card_id);

    bool InserNewCarInfo(QSqlDatabase* db, QString card_id,WeightInfo Info,CardInfo card_info);

    bool UpdateCarInfo(QSqlDatabase* db, QString card_id, WeightInfo Info);

    bool IsHaveCardId(QSqlDatabase* db, QString card_id);

    bool InserNewCardAndWeight(QSqlDatabase* db, QString card_id, WeightInfo &info, CardInfo &car_info, CarType &car_type);

    bool IsNewInCar(QSqlDatabase* db, QString card_id);

    bool IsDownWeight(QSqlDatabase*db, QString card_id,WeightInfo &info, CardInfo &car_info, CarType &car_type);

    bool FindUpWeightTimes(QSqlDatabase* db,QString card_id,int& times);

    bool SelectFirstTime(QSqlDatabase* db, QString card_id, QDateTime& datetime);

    bool UpdateManyWeight(QSqlDatabase* db, QString card_id, WeightInfo Info);

    bool UpdateOutFactoryStatus(QSqlDatabase* db,QString card_id);

    bool OnceWeightComplete(QSqlDatabase* db,QString card_id);

    bool UpdateImage(QSqlDatabase* db, const QString& card_id, const QString& imagePath, bool IsOut);

    bool IsOutFactory(QSqlDatabase* db,const QString& card_id);

    bool SureOut(QSqlDatabase* db, const QString &card_id);

    bool IsPrintList(QSqlDatabase* db,QString card_id);

    bool SureDown(QSqlDatabase* db,QString card_id);

    bool IsHaveUnusualCar(QSqlDatabase* db,const QString& car_plate);

    bool UpdateUnusualCar(QSqlDatabase* db,WeightInfo &info,const QString &car_plate);

    bool UpdateUnusualPic(QSqlDatabase* db, const QString& card_id, const QString& imagePath);

    bool InsertUnusualCar(QSqlDatabase* db,WeightInfo &info,const QString &car_plate);

    bool UnusualCarDown(QSqlDatabase* db, const QString& card_id);
public slots:
    bool saveoneCarInfo(QSqlDatabase *db,CarInfStr carInfo);
    QString  get_random_number();
    bool updateOneCarInfo(QSqlDatabase *db,QString cpflag,QString cId,QString p);
    bool selectoneCarRecord(QSqlDatabase *db,QString car_no,QString &cid);
    QString ParkCarOutJson(RecordInf *info);

    bool selectOneRecord(QSqlDatabase *db,RecordInf *info,QString id);
    bool getOneTableInfo(QSqlDatabase *db,RecordInf *info,QString cId,QString tableName);
    const char * StrToChar(QString str);

    bool selectTable(QSqlDatabase *db,QString &recordId,QString &tableName);
    bool deleteTable(QSqlDatabase *db,QString recordId,QString tableName);

    bool updateAxis(QSqlDatabase *db,AxisInfo *info);
    bool updateWeight(QSqlDatabase *db, WeightInfo *info);
    bool updateDynamicWeight(QSqlDatabase *db, DynamicWeightInfo *info, AxisInfo *Ainfo);



    bool updateOneRecord(QSqlDatabase *db,QString id,QString num);

    QByteArray Image_To_Base64(QString ImgPath);
    QString getXml(RecordInf *info);

    bool saveWaitUpInfo(QSqlDatabase *db,QString cId,QString tabelaName);

    QString RecordXml(RecordInf *info);
    void compressImg(QString fileName);
    void moveImageFile(QString filename);

    static bool deleteone(QSqlDatabase *db, int Number);
	//查询皮重表内是否有CardId
	bool isSkinNewCarWeight(QSqlDatabase *db, QString CardId);
	//更新皮重
    bool updateSkinWeight(QSqlDatabase *db, QString CardId, WeightInfo& info);
	//插入卡皮重
    bool insertSkinCardWeight(QSqlDatabase *db, QString CardId, WeightInfo& info);
	//查询上层设置标志
    bool isSkinSetFlag(QSqlDatabase *db, QString CardId);
    //查看是否有空车
    bool isSkinHaveNone(QSqlDatabase *db);

    bool insertNoneCarWht(QSqlDatabase *db, WeightInfo& info);
    bool undateNoneCarWht(QSqlDatabase *db, WeightInfo& info);
    //查询是否设定磅上状态
    bool isINbangFlag(QSqlDatabase *db);
    //插入一个新的卡和皮重
    bool insertHeadNewCar(QSqlDatabase *db, QString card_id, WeightInfo& info,CardInfo& car_info,CarType& car_type);
    //更新卡的皮重
    bool updateHeadNewCar(QSqlDatabase *db, QString CardId, WeightInfo& info);

    bool IsUpdateCarTare(QSqlDatabase *db, QString CardId);

    bool insertLbNoneCarTare(QSqlDatabase *db, WeightInfo &info);

    bool isHaveNone(QSqlDatabase *db);

    bool updateLbNoneCarTare(QSqlDatabase *db, WeightInfo &info);

    bool isSetTareFlag(QSqlDatabase *db, QString CardId);

    bool InSetDownCar(QSqlDatabase *db, QString CardId);

    bool InSetDownNoneCar(QSqlDatabase *db);
    //初始化设置，清除is_down状态
    bool ClearIsDown(QSqlDatabase *db);

    QString GetIp(QSqlDatabase *db);
};
//static datebase *datebase::GetInstance;
#endif // DATEBASE_H
